using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing.Design;
using System.Drawing;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

namespace SCS.Web.Controls.Design
{
	public class ItemCssClassConverter : ExpandableObjectConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
		{
			if (t == typeof(string))
				return true;

			return base.CanConvertFrom(context, t);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) 
		{
			if (destType == typeof(InstanceDescriptor) || destType == typeof(string)) 
			{
				return true;
			}
			return base.CanConvertTo(context, destType);
		}	
		
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo info, object value)
		{
			if (value is string)
			{
				string styleClassList = (string)value;
					
				try
				{
					string[] pairs = styleClassList.Split(info.TextInfo.ListSeparator[0]);
					string[] args = new string[5];

					foreach (string pair in pairs)
					{
						string name = pair.Split('=')[0].Trim();
						string val = pair.Split('=')[1].Trim();

						name = name.Trim();
						val = val.Trim();

						switch (name)
						{
							case "Over":
								args[1] = val;
								break;

							case "Selected":
								args[2] = val;
								break;

							case "SelectedOver":
								args[3] = val;
								break;

							case "Disabled":
								args[4] = val;
								break;

							default:
								args[0] = val;
								break;
						}
					}

					return new ItemCssClass(args[0], args[1], args[2], args[3], args[4]);
				}
				catch
				{
					return new ItemCssClass();
				}				
			}
			return base.ConvertFrom(context,info,value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo info, object value, Type destType )
		{
			if (destType == typeof(string))
			{
				ItemCssClass style = (ItemCssClass)value;
				
				string text = string.Empty;

				if (style.Normal.Length > 0)
					text += string.Format("{0} {1}", 
						info.TextInfo.ListSeparator[0], style.Normal);

				if (style.Over.Length > 0)
					text += string.Format("{0} Over={1}", 
						info.TextInfo.ListSeparator[0], style.Over);

				if (style.Selected.Length > 0)
					text += string.Format("{0} Selected={1}", 
						info.TextInfo.ListSeparator[0], style.Selected);

				if (style.SelectedOver.Length > 0)
					text += string.Format("{0} SelectedOver={1}", 
						info.TextInfo.ListSeparator[0], style.SelectedOver);

				if (style.Disabled.Length > 0)
					text += string.Format("{0} Disabled={1}", 
						info.TextInfo.ListSeparator[0], style.Disabled);
				
				if (text.Length > 0)
					text = text.Substring(1);

				return text;
			}
			else if (destType == typeof(InstanceDescriptor)) 
			{
				Type[] types = new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) };
				ConstructorInfo constructorInfo = typeof(ItemCssClass).GetConstructor(types);
				
				ItemCssClass style = (ItemCssClass)value;
				object[] args = new object[] { style.Normal, style.Over, style.Selected, style.SelectedOver };

				InstanceDescriptor descriptor = new InstanceDescriptor(constructorInfo, args, true);

				return descriptor;
			}
			return base.ConvertTo(context, info, value, destType);
		}
	}
}



